package sol.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * Combinations
 */
public class Issue077 {
    public List<List<Integer>> combine(int n, int k) {
        return combine(n, k, 1);
    }

    private List<List<Integer>> combine(int n, int k, int from) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();

        if (k == 0) {
            res.add(new ArrayList<Integer>());
            return res;
        }

        for (int i = from; i <= n - k + 1; ++i) {
            List<List<Integer>> tmp = combine(n, k - 1, i + 1);

            for (List<Integer> comb : tmp) {
                comb.add(0, i);
                res.add(comb);
            }

        }

        return res;
    }

    public static void main(String[] args) {
        Issue077 sol = new Issue077();
        List<List<Integer>> res = sol.combine(5, 2);
        for (List<Integer> r : res)
            System.out.println(r);
    }
}
